package com.arvato.common.ftp;

import java.io.File;
import java.io.InputStream;

import javax.servlet.ServletOutputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import com.arvato.common.config_bean.FtpConfig;

/**
 * 
 * @author: smallking
 * 
 * @Descibe:ftp操作控制类
 * 
 * @Date: 2018年5月15日 下午5:07:39
 */
@Component
public class FtpTool {
	
	Logger logger = LoggerFactory.getLogger(FtpTool.class);

	/**
	 * 上传文件到ftp服务器
	 * 
	 * @param file
	 * @return
	 */
	public String uploadFile(File file) {
		try (FtpUtil ftpUtil = new FtpUtil(FtpConfig.FTP_HOST, FtpConfig.FTP_PORT,
				FtpConfig.FTP_USERNAME, FtpConfig.FTP_PASSWORD)) {
			String fileName = file.getName();
			boolean flag = ftpUtil.uploadFile(file.getName(), file);
			logger.info("上传结果" + flag);
			if (flag) {
				return fileName;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 上传文件到ftp服务器
	 * 
	 * @param mFile
	 * @return
	 */
	public String uploadFile(MultipartFile mFile) {
		try {
			@SuppressWarnings("resource")
			FtpUtil ftpUtil = new FtpUtil(FtpConfig.FTP_HOST, FtpConfig.FTP_PORT,
					FtpConfig.FTP_USERNAME, FtpConfig.FTP_PASSWORD);
			String originFIleName = mFile.getOriginalFilename();
			String fileName = System.currentTimeMillis() + originFIleName.substring(originFIleName.lastIndexOf("."));
			ftpUtil.changeDirectory(FtpConfig.FTP_BASEPATH);
			boolean flag = ftpUtil.uploadFile(fileName, mFile.getInputStream());
			logger.info("上传结果" + flag);
			if (flag) {
				return fileName;
			}
		} catch (Exception e) {
			e.printStackTrace();
			
		}
		return null;
	}

	/**
	 * 上传文件到ftp服务器
	 * 
	 * @param inputStream
	 * @param fileName
	 * @return
	 */
	public boolean uploadFile(InputStream inputStream, String fileName) {
		try (FtpUtil ftpUtil = new FtpUtil(FtpConfig.FTP_HOST, FtpConfig.FTP_PORT,
				FtpConfig.FTP_USERNAME, FtpConfig.FTP_PASSWORD)) {
			ftpUtil.changeDirectory(FtpConfig.FTP_BASEPATH);
			boolean flag = ftpUtil.uploadFile(fileName, inputStream);
			logger.info("上传结果" + flag);
			return flag;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 查看ftp服务器的图片
	 * 
	 * @param outputStream
	 * @param fileName
	 */
	public void view(ServletOutputStream outputStream, String fileName) {
		try (FtpUtil ftpUtil = new FtpUtil(FtpConfig.FTP_HOST, FtpConfig.FTP_PORT,
				FtpConfig.FTP_USERNAME, FtpConfig.FTP_PASSWORD)) {
			ftpUtil.changeDirectory(FtpConfig.FTP_BASEPATH);
			ftpUtil.downloadFile(fileName, outputStream);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 删除远程ftp服务器上的文件
	 * 
	 * @param fileName
	 * @return
	 */
	public boolean delete(String fileName) {
		long start = System.currentTimeMillis();
		try (FtpUtil ftpUtil = new FtpUtil(FtpConfig.FTP_HOST, FtpConfig.FTP_PORT,
				FtpConfig.FTP_USERNAME, FtpConfig.FTP_PASSWORD)) {
			ftpUtil.changeDirectory(FtpConfig.FTP_BASEPATH);
			boolean flag = ftpUtil.delete(fileName);
			logger.info("删除" + fileName + "结果：" + flag);
			long end = System.currentTimeMillis();
			logger.info("删除耗时:" + (end - start));
			return flag;
		} catch (Exception e) {
			e.printStackTrace();
			long end = System.currentTimeMillis();
			logger.info("删除耗时:" + (end - start));
		}
		return false;
	}

}
